package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.mo.MessageObject;
import cn.zj.logistics.pojo.BaseData;
import cn.zj.logistics.pojo.Customer;
import cn.zj.logistics.pojo.CustomerExample;
import cn.zj.logistics.pojo.User;
import cn.zj.logistics.pojo.UserExample;
import cn.zj.logistics.pojo.CustomerExample.Criteria;
import cn.zj.logistics.service.BaseDataService;
import cn.zj.logistics.service.CustomerService;
import cn.zj.logistics.service.UserService;
import cn.zj.logistics.utils.Constant;

@Controller
@RequestMapping("/customer")
public class CustomerController {

	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private UserService userService;
	
	
	@Autowired
	private BaseDataService baseDataService;

	// 页面跳转功能
	@RequestMapping("/customerPage")
	@RequiresPermissions("customer:customerPage")
	public String customerPage() {

		return "customerPage";
	}

	@RequestMapping("/customerEdit")
	public String customerEdit(Model m, Long customerId) {

		// 如果用户id不等于空，说明是修改操作，查询用户对象共享过去，以供回显
		if (customerId != null) {
			Customer customer = customerService.selectByPrimaryKey(customerId);
			m.addAttribute("customer", customer);
		}
		
		//查询所有的业务员
		UserExample example = new UserExample();
		cn.zj.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRoleNameEqualTo(Constant.ROLE_SALESMAN);
		
		List<User> users = userService.selectByExample(example );
		m.addAttribute("users", users);
		
		//从基础数据表中查询区间数据
		List<BaseData> baseDatas = baseDataService.selectBaseDataByParentName(Constant.BASIC_COMMON_INTERVAL);
		m.addAttribute("baseDatas", baseDatas);
		return "customerEdit";
	}

	// 新增功能
	@RequestMapping("/insert")
	@RequiresPermissions("customer:insert")
	@ResponseBody
	public MessageObject insert(Customer customer) {

		MessageObject mo = MessageObject.createMessageObject(0, "新增数据失败，请联系管理员");

		int row = customerService.insert(customer);
		if (row == 1) {
			mo = MessageObject.createMessageObject(1, "新增数据成功");
		}

		return mo;
	}

	// 修改功能
	@RequiresPermissions("customer:update")
	@RequestMapping("/update")
	@ResponseBody
	public MessageObject update(Customer customer) {

		MessageObject mo = MessageObject.createMessageObject(0, "修改数据失败，请联系管理员");

		int row = customerService.updateByPrimaryKeySelective(customer);
		if (row == 1) {
			mo = MessageObject.createMessageObject(1, "修改数据成功");
		}

		return mo;
	}

	// 数据列表功能
	@ResponseBody
	@RequiresPermissions("customer:list")
	@RequestMapping("/list")
	public PageInfo<Customer> list(@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "10") int pageSize, String keyword) {

		PageHelper.startPage(pageNum, pageSize);

		CustomerExample example = new CustomerExample();
		// 基础数据名称查询
		Criteria criteria = example.createCriteria();
		
		//判断当前身份的角色是否是业务员
		//1.从Shiro中获取身份
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
		
		if(Constant.ROLE_SALESMAN.equals(user.getRolename())) {
			//只能查看当前业务的客户
			criteria.andUserIdEqualTo(user.getUserId());
			
		}
		
		
		// 条件搜索查询

		if (StringUtils.isNotBlank(keyword)) {
			
			criteria.andCustomerNameLike("%" + keyword + "%");

			Criteria criteria2 = example.createCriteria();
			criteria2.andAddressLike("%" + keyword + "%");

			example.or(criteria2);
		}
		List<Customer> customers = customerService.selectByExample(example);

		PageInfo<Customer> pageInfo = new PageInfo<Customer>(customers);
		return pageInfo;
	}

	@RequiresPermissions("customer:delete")
	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject delete(Long customerId) {

		// 创建消息对象
		MessageObject mo = MessageObject.createMessageObject(0, "删除数据失败，请联系管理员");
		int row = customerService.deleteByPrimaryKey(customerId);

		if (row == 1) {
			mo = MessageObject.createMessageObject(1, "删除数据成功");
		}

		return mo;
	}

}
